package com.hcb.micro.controller;

import java.io.OutputStream;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("excel")
public class ExcelController {
	/**
	 * 
	* @Title: statisticsList 
	* @Description: 财务报表-年
	* @return
	* @throws Exception
	* @throws
	 */
	@RequestMapping(value = "orderList", method = RequestMethod.GET)
	@ResponseBody
	public void excel(HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		HttpSession session = request.getSession();
		
		
		@SuppressWarnings("unchecked")
		List<Map<String,Object>> list = (List<Map<String, Object>>) session.getAttribute("ORDERLIST");
		
		response.setContentType("application/vnd.ms-excel");
		String codedFileName = "订单列表";
		OutputStream fOut = null;
		HSSFWorkbook workbook = null;
		try {
			// 进行转码，使其支持中文文件名
			codedFileName = java.net.URLEncoder.encode(codedFileName, "UTF-8");
			response.setHeader("content-disposition", "attachment;filename=" + codedFileName + ".xls");
			// 产生工作簿对象
			workbook = new HSSFWorkbook();
			// 产生工作表对象
			HSSFSheet sheet = workbook.createSheet();
			String[] str = { "序号","订单编号", "基本信息", "详细信息","订单状态","物流信息","订单归属","订单备注"};
			Row row = sheet.createRow(0);
			for (int i = 0; i < str.length; i++) {
				HSSFCellStyle cellStyle=workbook.createCellStyle();       
				cellStyle.setWrapText(true);       
				
				Cell cell = row.createCell(i);
				cell.setCellValue(str[i]);
				cell.setCellStyle(cellStyle);
			}
			if (list != null) {
				List<Map<String,Object>> rightList = list;
				int index = 1;
				int number = 1;
				for (Map<String,Object> map : rightList) {
					Row r = sheet.createRow(index++);
					r.createCell(0).setCellValue(number++);
					r.createCell(1).setCellValue(map.get("number")+"");
					r.createCell(2).setCellValue("下单时间："+map.get("createTime") + "/r/n"+"付款时间："+ map.get("payTime")+ "/r/n"+"订单总价："+ map.get("totalPrice")+ "/r/n"+ "实付金额："+ map.get("realPrice") + "");
					r.createCell(3).setCellValue("微信昵称："+map.get("nickname") + "/r/n"+"收货人："+ map.get("fullName")+ "/r/n"+"收货手机："+ map.get("mobileNumber")+ "/r/n"+ "身份证："+ map.get("idCard") + "");
					r.createCell(4).setCellValue(map.get("status") + "");
					r.createCell(5).setCellValue("发货时间："+map.get("logisticsTime") + "/r/n"+"物流公司："+ map.get("logisticsChannel")+ "/r/n"+"运单号："+ map.get("logisticsNumber")+ "");
					r.createCell(6).setCellValue(map.get("belong") + "");
					r.createCell(7).setCellValue(map.get("remark") + "");
				}
			}
			fOut = response.getOutputStream();
			workbook.write(fOut);
			fOut.flush();
		} catch (Exception e) {
			response.setCharacterEncoding("utf-8");
			response.setContentType("text/html;charset=utf-8");
			response.setHeader("content-disposition", "");
			if (fOut == null) {
				fOut = response.getOutputStream();
			}
			String errorStr = "<script>alert('系统出错');window.history.back();</script>";
			fOut.write(errorStr.getBytes());
			fOut.flush();
		} finally {
			if (fOut != null) {
				try {
					fOut.close();
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
			if (workbook != null) {
				try {
					workbook.close();
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}
	}
}
